package com.hzdesk.media.koreabatchmatvector.fragment.fragment_persenter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.InputDevice;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.hzdesk.media.koreabatchmatvector.R;
import com.hzdesk.media.koreabatchmatvector.base.MyClientApplication;
import com.hzdesk.media.koreabatchmatvector.bean.MouseUserDevicesinfoBean;
import com.hzdesk.media.koreabatchmatvector.bean.MousetBean;
import com.hzdesk.media.koreabatchmatvector.bean.UserBean;
import com.hzdesk.media.koreabatchmatvector.config.RxBusUtil;
import com.hzdesk.media.koreabatchmatvector.fragment.fragment_contract.MouseSetContract;
import com.hzdesk.media.koreabatchmatvector.fragment.fragment_modle.MouseSetModle;
import com.hzdesk.media.koreabatchmatvector.matvector.NiceSpinner;
import com.hzdesk.media.koreabatchmatvector.utils.RxBus;
import com.orhanobut.logger.Logger;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by Administrator on 2018/6/22.
 * 杭州席媒科技有限公司  Developers  刘传华  19
 * 佛祖镇楼 bug辟易
 */

public class MouseSetPersenter  implements MouseSetContract.IMouseSetPersenter{

    private MouseSetContract.IMouseSetView iMouseSetView ;
    private int currentPosition,colorPosition,sizePosition;                                                        //当前用户 ,当前颜色 ,当前笔触
    private boolean isChecked = false;
    List<String> datalist_1 = new LinkedList<>(Arrays.asList("红", "橙", "黄", "绿", "天蓝", "蓝", "紫", "黑"));  //颜色下拉框数据
    List<String> datalist_3 = new LinkedList<>(Arrays.asList("red_line", "orange_line", "yellow_line", "green_line", "cyan_line", "blue_line", "puple_line", "black_line"));  //注意颜色字母第一个大写
    List<String> datalist_2 = new LinkedList<>(Arrays.asList("1", "3", "5", "7", "9"));                         //笔触下拉框数据
    List<String> datalist_number = new LinkedList<>(Arrays.asList("一", "二", "三", "四", "五", "六", "七", "八")); //鼠标标号数据
    private MouseSetContract.IMouseSetModle iMouseSetModle;
    private NiceSpinner tNiceSpinnerOne,tNiceSpinnerTwo;
    private MouseUserDevicesinfoBean touseUserDevicesinfoBean ;
    public MouseSetPersenter(MouseSetContract.IMouseSetView tMouseSetView) {
        this.iMouseSetView = tMouseSetView;
        iMouseSetModle = new MouseSetModle();
    }
    @Override
    public void initDate(NiceSpinner mNiceSpinnerOne, NiceSpinner mNiceSpinnerTwo, Context mContext) {
        tNiceSpinnerOne =mNiceSpinnerOne;
        tNiceSpinnerTwo = mNiceSpinnerTwo;
        mNiceSpinnerOne.attachDataSource(datalist_1);
        mNiceSpinnerTwo.attachDataSource(datalist_2);
        mNiceSpinnerOne.setOnItemSelectedListener(itemSelectedListener_1);
        mNiceSpinnerTwo.setOnItemSelectedListener(itemSelectedListener_2);
    }

    @Override
    public void setinitListener(final CheckBox mCheckBox, final Context mContext) {
        // todo 这里可能有问题?
        RxBus.getInstance().subscribe(MousetBean.class, new io.reactivex.functions.Consumer() {
            @Override
            public void accept(Object o) throws Exception {
                MousetBean bean = (MousetBean) o;
                updateView(bean.getPosition(),mCheckBox,mContext);
            }
        });
    }

    @Override
    public void setOnGenericMotionListener(NiceSpinner mNiceSpinnerOne, NiceSpinner mNiceSpinnerTwo) {
        mNiceSpinnerOne.setOnGenericMotionListener(onGenericMotionListener_1);
        mNiceSpinnerTwo.setOnGenericMotionListener(onGenericMotionListener_2);
    }

    @Override
    public void onClickCancle(int pager) {
        iMouseSetView.sendMessage(pager);
    }

    @Override
    public void onClickConfirm(Context mContext) {
        try {
            String name = iMouseSetView.getMouseSetName();
            int DisMouse = isChecked? 1:0;
            String CursorColor  =   datalist_3.get(colorPosition);
            int BrushSize = Integer.parseInt(datalist_2.get(sizePosition));
            //将对应鼠标用户的信息更新修改数据库
            if(touseUserDevicesinfoBean != null){
                touseUserDevicesinfoBean.setName(name);
                touseUserDevicesinfoBean.setDisMouse(DisMouse);
                touseUserDevicesinfoBean.setCursorColor(CursorColor);
                touseUserDevicesinfoBean.setBrushSize(BrushSize);
                // 更新数据库
                iMouseSetModle.update(touseUserDevicesinfoBean,mContext);
            }
        } catch (NumberFormatException e) {}
        iMouseSetView.sendMessage(RxBusUtil.CHANGE_MOUSE_LIST_FRAGMENR);
    }

    //  根据几号笔触 显示对应的信息 几号鼠标 笔触颜色索引 是否点击 选中事件 取消 确定
    private void updateView(int position, CheckBox mCheckBox,Context mContext) {
        currentPosition = position;
        // 从数据库获取 对应的鼠标数据展示
        List<MouseUserDevicesinfoBean>  datelist =  iMouseSetModle.query(mContext);
        // 遍历集合
        if(datelist != null && datelist.size() >0){
            // 查询对应的鼠标展示鼠标各种信息
            for (MouseUserDevicesinfoBean mouseUserDevicesinfoBean : datelist) {
                    if(mouseUserDevicesinfoBean.getMouseNum() == (position+1 )){
                        touseUserDevicesinfoBean = mouseUserDevicesinfoBean;
                        iMouseSetView.setTvNumber(datalist_number.get(position) + "号鼠标");
                        mCheckBox.setOnCheckedChangeListener(onCheckedChangeListener); //mouseUserDevicesinfoBean.getDisMouse() ==0?false:true
                        mCheckBox.setChecked(mouseUserDevicesinfoBean.getDisMouse() ==0?false:true);
                        //展示用户名称  光标样式  笔触大小
                        iMouseSetView.setupdateView(mouseUserDevicesinfoBean);
                        tNiceSpinnerOne.setSelectedIndex(datalist_3.indexOf(mouseUserDevicesinfoBean.getCursorColor()));
                        tNiceSpinnerTwo.setSelectedIndex(datalist_2.indexOf(mouseUserDevicesinfoBean.getBrushSize()+""));
                    }
            }
        }else{
            iMouseSetView.setMouseSetName(datalist_number.get(currentPosition)+"号用户");
            tNiceSpinnerOne.setSelectedIndex(2);
            tNiceSpinnerTwo.setSelectedIndex(2);
        }
    }
    // todo 监听===========================================================================
    // 下拉框点击事件
    private AdapterView.OnItemSelectedListener itemSelectedListener_1 = new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            colorPosition = position;

        }
        @Override
        public void onNothingSelected(AdapterView<?> parent) {}
    };
    // 下拉框点击事件
    private  AdapterView.OnItemSelectedListener itemSelectedListener_2 = new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            sizePosition = position;
        }
        @Override
        public void onNothingSelected(AdapterView<?> parent) {}
    };
    // 选中事件
    private CompoundButton.OnCheckedChangeListener onCheckedChangeListener = new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean ischecked) {
            isChecked = ischecked;
        }
    };
    // 鼠标滚动事件
    private View.OnGenericMotionListener onGenericMotionListener_1 = new View.OnGenericMotionListener() {
        @Override
        public boolean onGenericMotion(View v, MotionEvent event) {
            if (0 != (event.getSource() & InputDevice.SOURCE_CLASS_POINTER)) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_SCROLL:
                        if (event.getAxisValue(MotionEvent.AXIS_VSCROLL) < 0.0f) {
                            if (colorPosition < 7) {
                                colorPosition++;
                            }
                        } else {
                            if (colorPosition > 0) {
                                colorPosition--;
                            }
                        }
                        iMouseSetView.setOneSelectedIndex(colorPosition);
                        break;
                }
            }
            return false;
        }
    };
    // 鼠标滚动事件
    private View.OnGenericMotionListener onGenericMotionListener_2 = new View.OnGenericMotionListener() {
        @Override
        public boolean onGenericMotion(View v, MotionEvent event) {
            if (0 != (event.getSource() & InputDevice.SOURCE_CLASS_POINTER)) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_SCROLL:
                        if (event.getAxisValue(MotionEvent.AXIS_VSCROLL) < 0.0f) {
                            if (sizePosition < 4) {
                                sizePosition++;
                            }
                        } else {
                            if (sizePosition > 0) {
                                sizePosition--;
                            }
                        }
                        iMouseSetView.setTwoSelectedIndex(sizePosition);
                        break;
                }
            }
            return false;
        }
    };


}
